package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.JsonFormatUtil
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: apiWOSendNotice供动态页面发送工单状态通知* @author: LiJuntao* @date 2021-07-13 23:58:55
 *
 */

public class apiWOSendNotice implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiWOSendNotice.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        // 接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + JsonFormatUtil.formatJson(callJson));

        try {
            //解析Json
            JSONObject inJson = new JSONObject(callJson);

            String send_type = KHUtil.GetJV(inJson.getJSONObject("data"), "send_type");// 工单待处理 || 受理进度
            String send_target = KHUtil.GetJV(inJson.getJSONObject("data"), "send_target");// 发送对象 customer staff rolegroup all
            String workorder_guid = KHUtil.GetJV(inJson.getJSONObject("data"), "workorder_guid");// 要发送的工单
            String elitetask_guid = KHUtil.GetJV(inJson.getJSONObject("data"), "elitetask_guid");// 要发送的工单当send_type为finish/customer时此参数忽略
            String user_id = KHUtil.GetJV(inJson.getJSONObject("data"), "user_id"); // 发送人 当send_type为finish/customer时此参数忽略
            String send_remark = KHUtil.GetJV(inJson.getJSONObject("data"), "send_remark");// 发送备注说明
            String send_url = KHUtil.GetJV(inJson.getJSONObject("data"), "send_url");// 发送的URL，如果有值 则给客户发送此URL，如果空则给客户发送工单短链接
            String role_id = KHUtil.GetJV(inJson.getJSONObject("data"), "role_id");// 发送的角色id

            if (KHUtil.isNull(send_target)) {
                send_target = send_type;
                send_type = "受理进度";
            }

            log.debug("请求参数-查询条件-send_target:" + send_target + " send_type:" + send_type + " role_id:" + role_id + " workorder_guid:" + workorder_guid + " elitetask_guid:" + elitetask_guid + " user_id:" + user_id + " send_remark:" + send_remark + " send_url:" + send_url);

            // 非空校验检查检查
            String isNullMSG = KHUtil.isNullMSG("send_type", send_type) + KHUtil.isNullMSG("send_target", send_target) + KHUtil.isNullMSG("workorder_guid", workorder_guid);
            if (!KHUtil.isNull(isNullMSG)) {
                result.put("code", -1);
                result.put("msg", isNullMSG);
                return result.toString();

            }

            if (send_target == "customer") {
                // 通知客户后续处理
                result = customerSend(eliteBean, send_type, workorder_guid, elitetask_guid, user_id, send_remark, send_url, send_target);

            } else if (send_target == "staff") {
                // 通知员工后续处理
                result = staffSend(eliteBean, send_type, workorder_guid, elitetask_guid, user_id, send_remark, send_target, role_id);

            } else if (send_target == "rolegroup") {
                // 通知部门后续处理
                result = rolegroupSend(eliteBean, send_type, workorder_guid, elitetask_guid, user_id, role_id, send_remark, send_target);

            } else if (send_target == "all") {
                // 通知所有人
                result = rolegroupSend(eliteBean, send_type, workorder_guid, elitetask_guid, user_id, role_id, send_remark, send_target);

            } else {
                result.put("code", 1009);
                result.put("msg", "发送失败未知发送类型send_type：" + send_type);
                log.warn(result.getString("msg"));
            }

        } catch (Exception e) {
            log.error("发送信息异常-", e);
            result.put("code", 1008);
            result.put("msg", e.getMessage());
        }

        return result.toString();
    }

    // 发送客户通知
    public static JSONObject customerSend(EliteBean eliteBean, String send_type, String workorder_guid, String elitetask_guid, String user_id, String send_remark, String send_url, String send_target) {
        long startTime = System.currentTimeMillis();
        log.debug("处理发送客户通知:" + startTime);

        // 初始化返回参数
        JSONObject result = new JSONObject();

        try {
            String selectSQL = '''Select Distinct c.Customername, -- 客户姓名
                                                    o.Workorderid, -- 工单编号
                                                    Fm.Mapname, -- 工单类型
                                                    So.Objectivestatusdescription, -- 工单状态
                                                    o.businessnumber, -- 档位号
                                                    o.Expireddate, -- 最晚处理时间
                                                    o.Objective_Guid, -- 工单Guid
                                                    o.Customer_Guid, -- 客户Guid
                                                    c.Mobile, -- 客户手机
                                                    c.Open_Id, -- 客户微信
                                                    c.Wechatid, -- 客户小程序
                                                    Fmm.Groupidamobile
                                      From Objective o
                                      Left Join Elitetask t
                                        On t.Objective_Guid = o.Objective_Guid
                                      Left Join Wo3_Frame_Map Fm
                                        On Fm.Map_Id = o.Map_Id
                                      Left Join Wo3_Frame Fmm
                                        On Fm.Workorderframe_Id = Fmm.Workorderframe_Id
                                      Left Join Wo3_Node_Map Nm
                                        On o.Curstep_Id_Map = Nm.Nodemap_Id
                                       And Nm.Map_Id = Fm.Map_Id
                                      Left Join Customer c
                                        On o.Customer_Guid = c.Customer_Guid
                                      Left Join Sysc_Objectivestatus So
                                        On o.Objectivestatus = So.Objectivestatus
                                     where o.objective_guid = \'''' + workorder_guid + '''\'''';

            List selectResultList = eliteBean.chaxun_sql(selectSQL, "dataSource");
            if (selectResultList.size() <= 0) {

                result.put("code", 1007);
                result.put("msg", "未查询到工单&客户-发送结束！");
                return result;

            } else {
                Map selectResultMap = selectResultList.get(0);

                // 获取工单推送信息
                String Customername = selectResultMap.get("Customername");
                String Workorderid = selectResultMap.get("Workorderid");
                String Stalls_Code = selectResultMap.get("businessnumber");
                String Mapname = selectResultMap.get("Mapname");
                String Objstatus = selectResultMap.get("Objectivestatusdescription");
                String Expireddate = selectResultMap.get("Expireddate");

                // 获取客户基本信息
                String Objective_Guid = selectResultMap.get("Objective_Guid");
                String Customer_Guid = selectResultMap.get("Customer_Guid");
                String Mobile = selectResultMap.get("Mobile");
                String Open_Id = selectResultMap.get("Open_Id");
                String Wechatid = selectResultMap.get("Wechatid");

                // 其他信息
                String Source = selectResultMap.get("Source");
                String DynID = selectResultMap.get("Groupidamobile");

                log.debug("发送客户信息-Workorderid:" + Workorderid + " Mapname:" + Mapname + " Customername:" + Customername + " Mobile:" + Mobile + " Open_Id:" + Open_Id + " Wechatid:" + Wechatid);

                // 开始处理发送
                if (!KHUtil.isNull(Wechatid) && Source == "minip") {

                    // 获取工单对于的手机端界面获取NGS菜单短码
                    JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "Groovy-Inner") + "/api/apiShortenLink?dynid=" + DynID + "&userid=" + Customer_Guid + "&usertype=customer&woid=" +
                            Objective_Guid + "&taskid=&expand=&cbtype=&opsource=minip", "UTF-8", 5000, 0));

                    String linkURL = "pages/webView/workOrder/index?url=" + apiLinkJson.getString("message");

                    linkURL = KHUtil.isNull(send_url) ? linkURL : "pages/webView/workOrder/index?url=" + send_url;

                    // 发送小程序信息
                    String sendData = "";
                    String Template_Id = (send_type == "工单待处理") ? KHDBConfig.getParam(eliteBean, "WX3-T1") : KHDBConfig.getParam(eliteBean, "WX3-T2");
                    if (send_type == "工单待处理") {
                        sendData = woDealSend("小程序", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL, "");
                    } else {
                        sendData = woStatusSend("小程序", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL);
                    }

                    String Documentpath = "工单编号:" + Workorderid + "\n" +
                            "工单类型:" + Mapname + "\n" +
                            "工单状态:" + Objstatus + "\n" +
                            "备注:" + send_remark + "\n" +
                            "详情：" + linkURL;

                    sendDataDB(eliteBean, "5", Documentpath, sendData, send_remark, Customer_Guid, Objective_Guid, "minip", linkURL, Wechatid, Template_Id)
                    result.put("code", 1);
                    result.put("msg", "发送完成-类型:小程序");

                } else if (!KHUtil.isNull(Open_Id)) {
                    // 获取工单对于的手机端界面获取NGS菜单短码
                    JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "Groovy-Inner") + "/api/apiShortenLink?dynid=" + DynID + "&userid=" + Customer_Guid + "&usertype=customer&woid=" +
                            Objective_Guid + "&taskid=&expand=&cbtype=&opsource=wxc", "UTF-8", 5000, 0));

                    String linkURL = apiLinkJson.getString("message");
                    // linkURL = KHUtil.isNull(send_url) ? linkURL : send_url;
                    linkURL = send_url;

                    // 发送公众号信息
                    String sendData = "";
                    String Template_Id = (send_type == "工单待处理") ? KHDBConfig.getParam(eliteBean, "WX1-T1") : KHDBConfig.getParam(eliteBean, "WX1-T2");
                    if (send_type == "工单待处理") {
                        sendData = woDealSend("公众号", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL, "");
                    } else {
                        sendData = woStatusSend("公众号", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL);
                    }

                    String Documentpath = "工单编号:" + Workorderid + "\n" +
                            "工单类型:" + Mapname + "\n" +
                            "工单状态:" + Objstatus + "\n" +
                            "备注:" + send_remark + "\n" +
                            "详情：" + linkURL;

                    sendDataDB(eliteBean, "4", Documentpath, sendData, send_remark, Customer_Guid, Objective_Guid, "wxc", linkURL, Open_Id, Template_Id)
                    result.put("code", 1);
                    result.put("msg", "发送完成-类型:公众号");

                } else if (!KHUtil.isNull(Mobile)) {
                    // 获取工单对于的手机端界面获取NGS菜单短码
                    JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "Groovy-Inner") + "/api/apiShortenLink?dynid=" + DynID + "&userid=" + Customer_Guid + "&usertype=customer&woid=" +
                            Objective_Guid + "&taskid=&expand=&cbtype=&opsource=sms", "UTF-8", 5000, 0));

                    String linkURL = apiLinkJson.getString("message");
                    linkURL = KHUtil.isNull(send_url) ? linkURL : send_url;
                    // 发送短信信息
                    String sendData = "";
                    String sendJson = "";
                    String Template_Id = (send_type == "工单待处理") ? KHDBConfig.getParam(eliteBean, "SMS-T1") : KHDBConfig.getParam(eliteBean, "SMS-T2");
                    if (send_type == "工单待处理") {
                        sendData = woDealSend("短信", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL, "");
                    } else {
                        sendData = woStatusSend("短信", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL);
                    }
                    sendJson = woDealSendJson("短信", send_target, send_remark, Customername, Workorderid, Mapname, Objstatus, Stalls_Code, Expireddate, linkURL, send_type);

                    sendDataDB(eliteBean, "3", sendData, sendJson, send_remark, Customer_Guid, Objective_Guid, "sms", linkURL, Mobile, Template_Id)
                    result.put("code", 1);
                    result.put("msg", "发送完成-类型:短信");

                } else {
                    result.put("code", -2);
                    result.put("msg", "发送失败-无满足规则的发送途径信息Mobile:" + Mobile + " Open_Id:" + Open_Id + " MiniP_Id:" + Wechatid);
                }

            }

            long endTime = System.currentTimeMillis();
            log.debug("发送客户通知结束-时长(毫秒):" + (endTime - startTime));

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            String errorInfo = "发送客户通知异常-时长(毫秒):" + (endTime - startTime) + " 处理失败(" + e.getMessage() + ")";
            log.error(errorInfo);
            result.put("code", -12);
            result.put("msg", errorInfo);
        }
        return result;

    }

    // 发送员工通知
    public static JSONObject staffSend(EliteBean eliteBean, String send_type, String workorder_guid, String elitetask_guid, String user_id, String send_remark, String send_target, String in_role_id) {
        long startTime = System.currentTimeMillis();
        log.debug("开始处理发送员工通知:" + startTime);

        // 初始化返回参数
        JSONObject result = new JSONObject();

        try {

            String q_WhereSQL = "";
            if (!KHUtil.isNull(in_role_id)) {
                q_WhereSQL = "and (t.Handleby_Id = '" + user_id + "' and t.CUR_ROLEID='" + in_role_id + "')";
            } else {
                if (send_type == "水电完结通知") {
                    send_type = "受理进度";
                } else {
                    q_WhereSQL = "and t.Handleby_Id = '" + user_id + "'";
                }
            }

            // 获取工单基础信息
            String selectSQL = '''Select Distinct o.Workorderid, -- 工单编号
                                            o.Objective_Guid, -- 工单Guid
                                            t.Elitetask_Guid, -- 任务Guid
                                            o.Customer_Guid, -- 客户Guid
                                            Fm.Mapname, -- 工单类型
                                            Nm.Nodemapname, -- 步骤类型
                                            R1.Rolegroupdescription, -- 待处理组
                                            S1.Staffname, -- 待处理人
                                            So.Objectivestatusdescription, -- 工单状态
                                            t.Expectendtime, -- 最晚处理时间
                                            Tw.Stalls_Code, -- 档位号
                                            Tw.Source, -- 创建来源
                                            Yg.Staffname As Yg_Staffname,
                                            Yg.Mobile As Yg_Mobile,
                                            Yg.Openid As Yg_Openid,
                                            Fmm.Groupidamobile As Groupidamobile,
                                            Tw.tenant
                              From Objective o
                              Left Join Elitetask t
                                On o.Objective_Guid = t.Objective_Guid
                              Left Join Wo3_Frame_Map Fm
                                On Fm.Map_Id = t.Map_Id
                              Left Join Wo3_Frame Fmm
                                On Fm.Workorderframe_Id = Fmm.Workorderframe_Id
                              Left Join Wo3_Node_Map Nm
                                On t.Curstep_Id_Map = Nm.Nodemap_Id
                               And Nm.Map_Id = Fm.Map_Id
                              Left Join Customer c
                                On o.Customer_Guid = c.Customer_Guid
                              Left Join Rolegroup R1
                                On R1.Rolegroup_Id = t.Handlegroup_Id
                              Left Join Staff S1
                                On S1.Staff_Id = t.Handleby_Id
                              Left Join Sysc_Objectivestatus So
                                On o.Objectivestatus = So.Objectivestatus
                              Left Join Tabcwo_Business_Info Tw
                                On o.Objective_Guid = Tw.Relation_Id
                              Left Join Staff Yg
                                On 1 = 1
                               And Yg.Staff_Id = \'''' + user_id + '''\'
                             Where o.Objective_Guid = \'''' + workorder_guid + '''\'''' + q_WhereSQL;
            //And t.Handleby_Id = \'''' + user_id + '''\'''';

            List selectResultList = eliteBean.chaxun_sql(selectSQL, "dataSource");
            if (selectResultList.size() <= 0) {

                result.put("code", 1007);
                result.put("msg", "未查询到工单&员工-发送结束！");

            } else {
                Map selectResultMap = selectResultList.get(0);

                int resultCode = doSendStaff(eliteBean, selectResultMap, user_id, send_type, send_target, send_remark, "");
                result.put("code", resultCode);
                result.put("msg", "发送完成！");

            }

            long endTime = System.currentTimeMillis();
            log.debug("处理发送员工通知-时长(毫秒):" + (endTime - startTime) + " 结果：" + result.get("msg"));

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            String errorInfo = "处理发送员工通知-时长(毫秒):" + (endTime - startTime) + "处理失败(" + e.getMessage() + ")";
            log.error(errorInfo);
            result.put("code", -12);
            result.put("msg", errorInfo);
        }
        return result;
    }

    // 给员工发送消息
    public static int doSendStaff(EliteBean eliteBean, Map selectResultMap, String user_id, String send_type, String send_target, String send_remark, String tenant) {
        // 预定义发送结果
        int resultCode = 0;

        // 获取工单推送信息
        String Staffname = selectResultMap.get("Yg_Staffname");
        String Workorderid = selectResultMap.get("Workorderid");
        String Stalls_Code = selectResultMap.get("Stalls_Code");
        String Mapname = selectResultMap.get("Mapname");
        String Objstatus = selectResultMap.get("Objectivestatusdescription");
        String Expectendtime = selectResultMap.get("Expectendtime");

        // 获取客户基本信息
        String Objective_Guid = selectResultMap.get("Objective_Guid");
        String Elitetask_Guid = selectResultMap.get("Elitetask_Guid");
        String Customer_Guid = selectResultMap.get("Customer_Guid");
        String Mobile = selectResultMap.get("Yg_Mobile");
        String Open_Id = selectResultMap.get("Yg_Openid");
        String re_tenant = selectResultMap.get("tenant");

        // 其他信息
        String Source = selectResultMap.get("Source");
        String DynID = selectResultMap.get("Groupidamobile");

        log.debug("发送员工信息-Workorderid:" + Workorderid + " Mapname:" + Mapname + " Staffname:" + Staffname + " Mobile:" + Mobile + " Open_Id:" + Open_Id);

        // 开始处理发送
        if (!KHUtil.isNull(Open_Id)) {
            // 获取工单对于的手机端界面获取NGS菜单短码
            JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "Groovy-Inner") + "/api/apiShortenLink?dynid=" + DynID + "&userid=" + user_id + "&usertype=staff&woid=" +
                    Objective_Guid + "&taskid=" + Elitetask_Guid + "&expand=" + Elitetask_Guid + "&cbtype=&opsource=wxs", "UTF-8", 5000, 0));

            String linkURL = apiLinkJson.getString("message");

            // 发送公众号信息
            String sendData = "";
            String Template_Id = (send_type == "工单待处理") ? KHDBConfig.getParam(eliteBean, "WX2-T1") : KHDBConfig.getParam(eliteBean, "WX2-T2");
            if (send_type == "工单待处理") {
                sendData = woDealSend("公众号", send_target, send_remark, Staffname, Workorderid, Mapname, Objstatus, Stalls_Code, Expectendtime, linkURL, re_tenant);
            } else {
                sendData = woStatusSend("公众号", send_target, send_remark, Staffname, Workorderid, Mapname, Objstatus, Stalls_Code, Expectendtime, linkURL);
            }

            String Documentpath = "工单编号:" + Workorderid + "\n" +
                    "工单类型:" + Mapname + "\n" +
                    "工单状态:" + Objstatus + "\n" +
                    "备注:" + send_remark + "\n" +
                    "详情：" + linkURL;

            sendDataDB(eliteBean, "4", Documentpath, sendData, send_remark, Customer_Guid, Objective_Guid, "wxs", linkURL, Open_Id, Template_Id)
            resultCode = 1;
            log.debug("发送完成-类型:公众号");

        } else if (!KHUtil.isNull(Mobile)) {
            // 获取工单对于的手机端界面获取NGS菜单短码
            JSONObject apiLinkJson = new JSONObject(Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "Groovy-Inner") + "/api/apiShortenLink?dynid=" + DynID + "&userid=" + user_id + "&usertype=staff&woid=" + Objective_Guid + "&taskid=" + Elitetask_Guid + "&expand=" + Elitetask_Guid + "&cbtype=&opsource=sms", "UTF-8", 5000, 0));

            String linkURL = apiLinkJson.getString("message");

            // 发送短信信息
            String sendData = "";
            String sendJson = "";
            String Template_Id = (send_type == "工单待处理") ? KHDBConfig.getParam(eliteBean, "SMS-T1") : KHDBConfig.getParam(eliteBean, "SMS-T2");
            log.debug("发送完成-类型:短信");
            if (send_type == "工单待处理") {
                sendData = woDealSend("短信", send_target, send_remark, Staffname, Workorderid, Mapname, Objstatus, Stalls_Code, Expectendtime, linkURL, "");
            } else {
                sendData = woStatusSend("短信", send_target, send_remark, Staffname, Workorderid, Mapname, Objstatus, Stalls_Code, Expectendtime, linkURL);
            }
            sendJson = woDealSendJson("短信", send_target, send_remark, Staffname, Workorderid, Mapname, Objstatus, Stalls_Code, Expectendtime, linkURL, send_type);

            sendDataDB(eliteBean, "3", sendData, sendJson, send_remark, Customer_Guid, Objective_Guid, "sms", linkURL, Mobile, Template_Id)
            resultCode = 1;
            log.debug("发送完成-类型:短信");

        } else {
            resultCode = -2;
            log.debug("发送失败-无满足规则的发送途径信息Mobile:" + Mobile + " Open_Id:" + Open_Id);
        }

        return resultCode;
    }

    // 发送部门通知
    public static JSONObject rolegroupSend(EliteBean eliteBean, String send_type, String workorder_guid, String elitetask_guid, String user_id, String role_id, String send_remark, String send_target) {
        long startTime = System.currentTimeMillis();
        log.debug("开始处理发送部门通知:" + startTime);
        // 初始化返回参数
        JSONObject result = new JSONObject();
        try {

            // 获取工单基础信息
            String selectSQL = '''Select Distinct o.Workorderid, -- 工单编号
                                                o.Objective_Guid, -- 工单Guid
                                                t.Elitetask_Guid, -- 任务Guid
                                                o.Customer_Guid, -- 客户Guid
                                                Fm.Mapname, -- 工单类型
                                                Nm.Nodemapname, -- 步骤类型
                                                R1.Rolegroupdescription, -- 待处理组
                                                t.Handleby_Id, -- 待处理人ID
                                                S1.Staffname, -- 待处理人
                                                So.Objectivestatusdescription, -- 工单状态
                                                t.Expectendtime, -- 最晚处理时间
                                                Tw.Stalls_Code, -- 档位号
                                                Tw.Source, -- 创建来源
                                                Yg.Staffname                  As Yg_Staffname,
                                                Yg.Mobile                     As Yg_Mobile,
                                                Yg.Openid                     As Yg_Openid,
                                                Fmm.Groupidamobile            As Groupidamobile,
                                                Yg.Staff_Id                   As Staff_Id,
                                                Tw.tenant
                                  From Objective o
                                  Left Join Elitetask t
                                    On o.Objective_Guid = t.Objective_Guid
                                  Left Join Wo3_Frame_Map Fm
                                    On Fm.Map_Id = t.Map_Id
                                  Left Join Wo3_Frame Fmm
                                    On Fm.Workorderframe_Id = Fmm.Workorderframe_Id
                                  Left Join Wo3_Node_Map Nm
                                    On t.Curstep_Id_Map = Nm.Nodemap_Id
                                   And Nm.Map_Id = Fm.Map_Id
                                  Left Join Customer c
                                    On o.Customer_Guid = c.Customer_Guid
                                  Left Join Sysc_Priority Sp
                                    On t.Priority = Sp.Priority_Id
                                  Left Join Rolegroup R1
                                    On R1.Rolegroup_Id = t.Handlegroup_Id
                                  Left Join Staff S1
                                    On S1.Staff_Id = t.Handleby_Id
                                  Left Join Sysc_Objectivestatus So
                                    On o.Objectivestatus = So.Objectivestatus
                                  Left Join Tabcwo_Business_Info Tw
                                    On o.Objective_Guid = Tw.Relation_Id
                                  Left Join Staffrolegroup Sr
                                    On R1.Rolegroup_Id = Sr.Rolegroup_Id
                                  Left Join Staffrole sro
                                    On Sr.Staff_Id = sro.Staff_Id                                    
                                  Left Join Staff Yg
                                    On Sr.Staff_Id = Yg.Staff_Id
                                 Where Yg.App_Message = \'1\' 
                                   And o.Objective_Guid = \'''' + workorder_guid + '''\'
                                   And sro.role_id = \'''' + role_id + '''\'
                                   And t.Handlegroup_Id = \'''' + user_id + '''\'''';

            List selectResultList = eliteBean.chaxun_sql(selectSQL, "dataSource");
            if (selectResultList.size() <= 0) {

                result.put("code", 1007);
                result.put("msg", "未查询到待发送-发送结束！");

            } else {
                int allListNum = selectResultList.size();
                int allSendNum = 0;
                for (int i = 0; i < allListNum; i++) {
                    Map selectResultMap = selectResultList.get(i);
                    String Handleby_Id = selectResultMap.get("Staff_Id");

                    int resultCode = doSendStaff(eliteBean, selectResultMap, Handleby_Id, send_type, send_target, send_remark, "");
                    allSendNum += resultCode;

                }

                result.put("code", 1);
                result.put("msg", "处理发送部门通知发送完成！总待通知数量：" + allListNum + " 发送成功数量:" + allSendNum);
            }

            long endTime = System.currentTimeMillis();
            log.debug("处理发送部门通知完成-时长(毫秒):" + (endTime - startTime) + "处理结果:" + result.getString("msg"));

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            String errorInfo = "处理发送部门通知失败-时长(毫秒):" + (endTime - startTime) + "处理失败(" + e.getMessage() + ")";
            log.error(errorInfo);
            result.put("code", -12);
            result.put("msg", errorInfo);
        }

        return result;
    }

    // 工单待处理-通知
    public static String woDealSend(String Doctype, String send_target, String send_remark, String NickInfo, String Workorderid, String Mapname, String Objstatus, String Stalls_Code, String Expireddate, String ngsLink, String re_tenant) {
        long startTime = System.currentTimeMillis();
        log.debug("开始工单待处理-通知内容处理:" + startTime);
        String result = "";

        try {
            JSONObject sendData = new JSONObject();
            if (!KHUtil.isNull(send_remark)) {
                send_remark = Workorderid + "#" + send_remark;
            } else {
                send_remark = Workorderid;
            }
            if (Doctype == "小程序") {

                Map<String, String> dataV = new HashMap<String, String>();
                dataV.put("character_string25", Workorderid);
                dataV.put("thing6", Mapname);
                dataV.put("thing2", Objstatus);
                dataV.put("thing23", send_remark);

                result = KHUtil.minipTemplateData(dataV).toString();

            } else if (Doctype == "公众号") {

                String NickDesc = send_target == "customer" ? "女士/先生：" : "领导/同事：";

                Map<String, String> dataV = new HashMap<String, String>();
                /* dataV.put("woname", "尊敬的" + NickInfo + NickDesc);
                 dataV.put("woid", Workorderid);
                 dataV.put("wodwid", Stalls_Code);
                 dataV.put("wotime", Expireddate);
                 dataV.put("woremark", send_remark);*/


                dataV.put("first", "尊敬的" + NickInfo + NickDesc);
                dataV.put("keyword1", re_tenant);
                dataV.put("keyword2", Stalls_Code);
                dataV.put("keyword3", Expireddate);
                dataV.put("remark", send_remark);

                result = KHUtil.wxTemplateData(dataV).toString();

            } else {
                String NickDesc = send_target == "customer" ? "女士/先生：" : "领导/同事：";
                result = "尊敬的" + NickInfo + NickDesc + "您好！工单编号为" + Workorderid + "的" + Mapname + "工单待您处理，最晚处理时间为：" + Expireddate + "，请知悉并尽快处理！详情请查看：" + ngsLink;

            }

            long endTime = System.currentTimeMillis();
            log.debug("工单待处理-通知内容处理时长(毫秒):" + (endTime - startTime) + " 通知Data:" + result);

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("工单待处理-通知内容处理时长(毫秒):" + (endTime - startTime) + " 处理失败(" + e.getMessage() + ")");
        }

        return result;

    }

    // 受理进度通知-通知
    public static String woStatusSend(String Doctype, String send_target, String send_remark, String NickInfo, String Workorderid, String Mapname, String Objstatus, String Stalls_Code, String Expireddate, String ngsLink) {
        long startTime = System.currentTimeMillis();
        log.debug("开始受理进度-通知内容处理:" + startTime);
        String result = "";

        try {
            if (!KHUtil.isNull(send_remark)) {
                send_remark = send_remark;
            } else {
                send_remark = Mapname + "{" + Workorderid + "}号已走完相关流程，请知悉。";
            }
            JSONObject sendData = new JSONObject();
            if (Doctype == "小程序") {

                Map<String, String> dataV = new HashMap<String, String>();
                dataV.put("character_string25", Workorderid);
                dataV.put("thing6", Mapname);
                dataV.put("thing2", Objstatus);
                dataV.put("thing23", send_remark);

                result = KHUtil.minipTemplateData(dataV).toString();

            } else if (Doctype == "公众号") {

                String NickDesc = send_target == "customer" ? "女士/先生：" : "领导/同事：";

                Map<String, String> dataV = new HashMap<String, String>();
                /*dataV.put("woname", "尊敬的" + NickInfo + NickDesc);
                dataV.put("woid", Workorderid);
                dataV.put("wotype", Mapname);
                dataV.put("wostatus", Objstatus);
                dataV.put("woremark", send_remark);*/

                dataV.put("first", "尊敬的" + NickInfo + NickDesc);
                dataV.put("keyword1", NickInfo);
                dataV.put("keyword2", Stalls_Code);
                dataV.put("keyword3", Objstatus);
                dataV.put("remark", send_remark);

                result = KHUtil.wxTemplateData(dataV).toString();

            } else {
                String NickDesc = send_target == "customer" ? "女士/先生：" : "领导/同事：";
                result = "尊敬的" + NickInfo + NickDesc + "您好！工单编号为" + Workorderid + "的" + Mapname + "工单已处理完成！详情请查看：" + ngsLink;

            }

            long endTime = System.currentTimeMillis();
            log.debug("受理进度-通知内容处理时长(毫秒):" + (endTime - startTime) + " 通知Data:" + result);

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("受理进度-通知内容处理时长(毫秒):" + (endTime - startTime) + " 处理失败(" + e.getMessage() + ")");
        }

        return result;

    }

    //拼接短信发送Json报文
    public static String woDealSendJson(String Doctype, String send_target, String send_remark, String NickInfo, String Workorderid, String Mapname, String Objstatus, String Stalls_Code, String Expireddate, String ngsLink, String check_send_type) {
        String result = "";
        try {
            URL turl = new URL(ngsLink);
            ngsLink = turl.getFile().substring(2, turl.getFile().length());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        if (check_send_type == "工单待处理") {
            result = '{"staffname":"' + NickInfo + '","workorderid":"' + Workorderid + '","mapname":"' + Mapname + '","expectendtime":"' + Expireddate + '","linkurl":"' + ngsLink + '"}';
        } else {
            result = '{"staffname":"' + NickInfo + '","workorderid":"' + Workorderid + '","mapname":"' + Mapname + '","linkurl":"' + ngsLink + '"}';
        }
        return result;
    }

    // 发送数据DB处理
    public static void sendDataDB(EliteBean eliteBean, String Doctype, String Documentpath, String Documentpath2, String Comments, String Customer_Guid, String Objective_Guid, String Lockedby, String Stringfield1, String Openid, String Template_Id) {
        long startTime = System.currentTimeMillis();
        log.debug("开始将发送内容数据处理到DB:" + startTime);

        try {
            def InsertSQL = "Insert Into Documentout" +
                    "  (Documentout_Guid," +
                    "   Doctype," +
                    "   Status," +
                    "   Trialcount," +
                    "   Trialcountlimit," +
                    "   Documentpath," +
                    "   Comments," +
                    "   Documentto," +
                    "   Priority_Id," +
                    "   Customer_Guid," +
                    "   Objective_Guid," +
                    "   Createdby," +
                    "   Createddate," +
                    "   Lockedby," +
                    "   Stringfield1," +
                    "   Documentpath2," +
                    "   Openid," +
                    "   Template_Id," +
                    "   Expectedstartdate)" +
                    "Values" +
                    "  (Uuid()," +
                    "   '" + Doctype + "'," +
                    "   3," +
                    "   0," +
                    "   3," +
                    "   '" + Documentpath + "'," +
                    "   '" + Comments + "'," +
                    "   '" + Openid + "'," +
                    "   '1'," +
                    "   '" + Customer_Guid + "'," +
                    "   '" + Objective_Guid + "'," +
                    "   'SELITE'," +
                    "   Now()," +
                    "   '" + Lockedby + "'," +
                    "   '" + Stringfield1 + "'," +
                    "   '" + Documentpath2 + "'," +
                    "   '福田农批'," +
                    "   '" + Template_Id + "'," +
                    "   Now())";
            eliteBean.update(InsertSQL, "dataSource");

            long endTime = System.currentTimeMillis();
            log.debug("发送内容数据处理到DB时长(毫秒):" + (endTime - startTime));

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            log.error("发送内容数据处理到DB时长(毫秒):" + (endTime - startTime) + " 处理失败(" + e.getMessage() + ")");
        }

    }

}